package com.changgou.system.service.impl;

import com.changgou.system.dao.AdminMapper;
import com.changgou.system.pojo.Admin;
import com.changgou.system.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;

/**
 * @author caixiaofeng
 * @company xxx公司科技有限公司
 * @create 2020-04-28 18:08
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public void add(Admin admin) {
        String password = BCrypt.hashpw(admin.getPassword(), BCrypt.gensalt());
        admin.setPassword(password);
        adminMapper.insert(admin);
    }

    @Override
    public boolean login(Admin admin) {
        Admin admin2 = new Admin();
        admin2.setLoginName(admin.getLoginName());
        admin2.setStatus("1");
        Admin admin3 = adminMapper.selectOne(admin2);
        if(admin3==null){
            return false;
        }else{
            String password = admin3.getPassword();
            boolean checkpw = BCrypt.checkpw(admin.getPassword(), admin3.getPassword());
            return checkpw;
        }
    }
}
